﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Pages_ConnectionList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            lblMessage.Text = String.Empty;

            if (!IsPostBack)
            {
                SetContentHeader();
                if (Session["user"] != null)
                {
                    if (Session["user"].ToString() == "DEMO")
                    {
                        gvConnectionList.DataSource = DBData.Connections(1);
                    }
                    else
                    {
                        gvConnectionList.DataSource = DBData.Connections(0);
                    }
                    gvConnectionList.DataBind();
                }
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }

    protected void btnNew_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Connection.aspx");
    }


    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
    }

    protected void gvConnectionList_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Set database connection
        //Session["ConnectionString"] = ((DataRowView)gvConnectionList.SelectedRow.DataItem)["ConnectionString"].ToString();
        Session["ConnectionId"] = gvConnectionList.SelectedDataKey.Values[0];
        Session["ConnectionString"] = gvConnectionList.SelectedDataKey.Values[1];
        Session["ConnectionName"] = gvConnectionList.SelectedDataKey.Values[2];
        lblMessage.Text = "Connection set for: " + Session["ConnectionString"].ToString();

        OutlookMaster om = (OutlookMaster)Master;
        om.ActiveConnection.Text = gvConnectionList.SelectedDataKey.Values[2].ToString();

        Response.Redirect("Connection.aspx?ConnectionId=" + gvConnectionList.SelectedDataKey.Values[0].ToString());
    }

    private void SetContentHeader()
    {
        OutlookMaster om = (OutlookMaster)Master;
        om.ContentHeader.Text = "Connection list";
    }
}